package com.cuc.platform.core.componet.security.dao.jdbc;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cuc.platform.core.componet.security.dao.SecurityMenuDao;
import com.cuc.platform.core.componet.security.domain.SecurityMenu;

/**
 * @author peepul
 *
 */
@Repository("SecurityMenuDao")
public class SecurityMenuDaoImpl  implements SecurityMenuDao {
		

	public SecurityMenuDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<SecurityMenu> getAllMenus() {

		final String sql = "select * from ss_menu ";
		List<SecurityMenu>  list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<SecurityMenu>(SecurityMenu.class));
		
		return list;
	}

	@Override
	public SecurityMenu getMenuById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertMenu(SecurityMenu menu) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateMenu(SecurityMenu menu) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public int delMenuById(Long id) throws DataAccessException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<SecurityMenu> listMenuByLevel(Integer level) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SecurityMenu> listSubMenu(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SecurityMenu> listMenuByUser(String userName) {
		StringBuilder sqlBud = new StringBuilder(); 
		sqlBud.append("SELECT DISTINCT		m.id, m.name, m.menu_url as menuUrl, m.parent_id as parentId, ");
		sqlBud.append("m.level, m.code, m.icon_url as iconUrl, m.is_admin as isAdmin, m.descn ");
		sqlBud.append(" FROM  ss_menu_role mr, ss_menu m ");
		sqlBud.append(" WHERE m.id = mr.menu_id " );
		sqlBud.append(" AND EXISTS ");
		sqlBud.append(" (SELECT 1 ");
		sqlBud.append(" FROM ss_user u, ss_user_role ur ");
		sqlBud.append(" WHERE u.id = ur.user_id ");
		sqlBud.append("  AND u.loginName = ? ");
		sqlBud.append("  AND ur.role_id = mr.role_id) ");
	 
		List<SecurityMenu> list =   jdbcTemplate.query(sqlBud.toString(), new String[]{userName}, new BeanPropertyRowMapper<SecurityMenu>(SecurityMenu.class));
		return list;
	}

	@Override
	public List<SecurityMenu> listMenuByUserWithLevel(
			Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SecurityMenu> viewRoleswithMenus(Long menu_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SecurityMenu> viewMenuswithRoles(Long role_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delRoleMenuUnions(Long role_id) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public void delRoleMenuUnionsByMenu(Long menu_id)
			throws DataAccessException {
		// TODO Auto-generated method stub

	}

	@Override
	public void saveRoleMenuUnions(Map<String, Long> map) {
		// TODO Auto-generated method stub

	}
	
	@Resource
	private JdbcTemplate jdbcTemplate;

}
